package com.test.semaphore.more1more;

import com.test.semaphore.MultiDoType;

import java.util.concurrent.Semaphore;
import java.util.concurrent.locks.ReentrantLock;

/**
 *
 * 多输入 - 单处理 - 多输出
 *
 *
 * @author:wesley
 */
public class More1More implements MultiDoType{

    private Semaphore semaphore = new Semaphore(3);
    private ReentrantLock lock = new ReentrantLock();

    @Override
    public void doService(){

        try {
            semaphore.acquire();
            //加入重入锁
            lock.lock();


            String name = Thread.currentThread().getName();
            System.out.println(name+"获得许可， 准备干活 。。");
            System.out.println("开始时间:" + System.currentTimeMillis());
            for (int i = 0; i < 5; i++) {
                System.out.println(name+"打印："+i);
            }

            System.out.println("结束时间:"+System.currentTimeMillis());

            lock.unlock();
            semaphore.release();

            System.out.println(name+"结束了");

        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }
}
